﻿using Newtonsoft.Json.Linq;
using NLog;
using ReportMealManagementSystem.Business;
using ReportMealManagementSystem.Entity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReportMealManagementSystem
{
    /// <summary>
    /// Announce.xaml 的交互逻辑
    /// </summary>
    public partial class Announce : UserControl
    {
        private Logger logger = LogManager.GetCurrentClassLogger();
        Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        int districtID = int.Parse(ConfigurationManager.AppSettings["districtID"]);
        BasicInfo basicInfo = new BasicInfo();
        JArray mealUsers = new JArray();

        public Announce()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 发送公告
        /// </summary>
        private void SendAnnounce()
        {
            string message = "";
            JObject joParam = new JObject();
            SystemParamSet.Dispatcher.Invoke(new Action(delegate {
                joParam["UserID"] = Global.MealUser.UserID;
                joParam["Title"] = txtTitle.Text.Trim();
                joParam["Text"] = gbtheText.ContentHtml;
                joParam["DistrictID"] = Global.MealUser.DistrictID;
            }));
            bool result = basicInfo.OptDataInfoNew(joParam, "gbt/Announce/OptAnnounce", ref message);
            this.message.Dispatcher.Invoke(new Action(delegate {
                this.message.Content = message;
            }));
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            txtTitle.Text = "";
            gbtheText.ContentHtml = "";
        }

        private void btnAnnounce_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtTitle.Text.Trim()))
            {
                return;
            }
            if (string.IsNullOrEmpty(gbtheText.ContentHtml))
            {
                return;
            }
            _loading.Visibility = Visibility.Visible;
            Task task = new Task(new Action(delegate {
                SendAnnounce();
            }));
            task.Start();
            task.ContinueWith((t) =>
            {
                _loading.Dispatcher.Invoke(new Action(delegate {
                    _loading.Visibility = Visibility.Collapsed;
                }));
            });
        }
    }
}
